package user

import (
	"context"
	"github.com/gogf/gf/v2/crypto/gmd5"
	"github.com/gogf/gf/v2/frame/g"
	"ticket/utils"
)

func (u UserApi) LoginUserInfo(ctx context.Context, req *LoginUserInfoReq) (res *LoginUserInfoRes, err error) {
	res = new(LoginUserInfoRes)
	info, valid := utils.CurrentUserInfo(ctx)
	if !valid {
		return nil, utils.NewError("根据Token获取用户信息出错")
	}
	res.UserInfo = info
	return
}

func (u UserApi) UpdatePass(ctx context.Context, req *UpdatePassReq) (res *UpdatePassRes, err error) {
	res = new(UpdatePassRes)
	var pass struct {
		Password string `json:"password"`
	}
	encryptedPassword, _ := gmd5.Encrypt(req.OldPassword)
	encrypt, _ := gmd5.Encrypt(req.NewPassword)
	// 检验原始密码
	err = g.Model("user_agents").Fields("password").Where("id = ?", req.Id).Scan(&pass)
	if pass.Password != encryptedPassword {
		return nil, utils.NewError("原始密码不正确")
	}
	// 这里可以执行修改密码的操作了
	g.Model("user_agents").Data(g.Map{
		"password": encrypt,
	}).Where("id = ?", req.Id).Update()
	return
}
